#!/usr/bin/env python3
# _*_ coding: utf-8 _*_


from ctypes import *
import platform


class PyFile(Structure):
	"""
    对应C中的FILE结构体
    """
	_fields_ = [("_ptr", c_char_p),
                ("_cnt", c_int),
                ("_base", c_char_p),
                ("_flag", c_int),
                ("_file", c_int),
                ("_charbuf", c_int),
                ("_bufsiz", c_int),
                ("_tmpfname", c_char_p)]


if __name__ == '__main__':
	# 普通结构体
	if platform.system() == 'Windows':
		lib_ext = '.dll'
	elif platform.system() == 'Linux':
		lib_ext = '.so'
	elif platform.system() == 'Darwin':
		lib_ext = '.dylib'
	libname = './libstdio_test' + lib_ext
	library = cdll.LoadLibrary(libname)

	# 这种方法好像只能传递1个字符
	# library.write_word.argtypes = [c_wchar_p]
	# library.write_word.restype = c_int
	# path = "word.txt"
	# path_s = c_wchar_p(path)
	# print(path_s)
	# print(path_s.value)
	# library.write_word(path_s)

	# 可以正常工作
	# library.write_word.argtypes = [c_char_p]
	# library.write_word.restype = c_int
	# path1 = create_string_buffer(b"word1.txt")
	# print(path1)
	# print(path1.value)
	# library.write_word(path1)

	# C函数用字符串数组，python用c_char_p
	library.write_console.argtypes = [c_char_p]
	library.write_console.restype = c_int
	word = create_string_buffer(b"hello console")
	library.write_console(word)